package close.kenny.lib.kwindows.shortcut
{
	import close.kenny.lib.kwindows.shortcut.events.ShortCutItemEvent;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.collections.IList;
	import mx.containers.HBox;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.core.FlexGlobals;
	import mx.core.IFactory;
	import mx.core.IVisualElement;
	import mx.events.CollectionEvent;
	
	import spark.components.BorderContainer;
	import spark.components.Button;
	import spark.components.Group;
	import spark.components.HGroup;
	import spark.components.Label;
	import spark.layouts.HorizontalLayout;

	[DefaultProperty("dataProvider")]
	public class ShortCut extends Group
	{
		private var _dataProvider:IList;
		
		[Bindable]
		private var fastList:Array=new Array();
		public function ShortCut()
		{
			super();
			var ho:HorizontalLayout=new HorizontalLayout;
			ho.verticalAlign="middle";
			this.layout=ho;
			this.height=80;
		}

		public function get dataProvider():IList
		{
			return this._dataProvider;
		}

		public function set dataProvider(value:IList):void
		{
			if (this._dataProvider == value) return;
			if (this._dataProvider != null) this._dataProvider.removeEventListener(CollectionEvent.COLLECTION_CHANGE, dataProviderListChangeHandler);
			this._dataProvider = value;
			this._dataProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE, dataProviderListChangeHandler);
			this.invalidateProperties();
		}
		public function onMove(event:MouseEvent):void
		{
			var point:Point=new Point(this.mouseX,this.mouseY);
//			point = this.localToGlobal(point);
//			point = this.globalToLocal(point);
			//	var point:Point = windowsContainer.parentApplication.globalToLocal(new Point(event.stageX, event.stageY));
			
			var minimumSize:Number = 32;
			var maximumSize:Number =64;
			var scaleRange:Number = 200;
			
			var insideDistance:Number = maximumSize / Math.SQRT2;
			
			for each (var render:IVisualElement in fastList) {
				if (render == null) continue;
				var renderPoint:Point = new Point(render.x + render.width/2, render.y + render.height/2);
				var distance:Number = Math.sqrt( Math.pow(renderPoint.x - point.x, 2) + Math.pow(renderPoint.y - point.y, 2) );
				
				var outerDistance:Number = render.width / Math.SQRT2;
				
				if (distance < insideDistance) {
					render.width = render.height = maximumSize;
					continue;
				}			
				
				if (distance > scaleRange + outerDistance) {
					render.width = render.height = minimumSize;
					continue;
				}
				
				var scale:Number = 1 - ( (distance-outerDistance) / scaleRange);
				var size:Number = minimumSize + (maximumSize - minimumSize) * scale;
				render.width = render.height = size;
			}
		}
		protected function dataProviderListChangeHandler(event:CollectionEvent):void 
		{
			this.invalidateProperties();
		}
		protected function createInstance(item:Object):IVisualElement {
			var instanceDock:ShortCutItem=new ShortCutItem();
			instanceDock.height=instanceDock.width=32;
			instanceDock.source=item.@icon;
			instanceDock.labelString=item.@label;
			instanceDock.urlString=item.@url;
			instanceDock.toolTip=item.@label;
			instanceDock.addEventListener(MouseEvent.CLICK,itemClickHanele);
			instanceDock.addEventListener(MouseEvent.MOUSE_OVER,itemOverHanele);
			instanceDock.addEventListener(MouseEvent.MOUSE_OUT,itemOutHanele);
			return instanceDock;
		}
		public function itemClickHanele(event:MouseEvent):void{
			dispatchEvent(new ShortCutItemEvent(ShortCutItemEvent.DOCK_ITEM_CLICK,event.currentTarget as ShortCutItem));
		}
		public function itemOverHanele(event:MouseEvent):void{
		}
		public function itemOutHanele(event:MouseEvent):void{
		}
		override protected function commitProperties():void {
			super.commitProperties();
			this.parentApplication.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
			if(dataProvider==null)
			{
				return;
			}
			else{
				for (var i:int = 0; i < this.dataProvider.length; i++) {
					var render:IVisualElement=createInstance(dataProvider.getItemAt(i));
					this.addElement(render);
					fastList.push(render);
				}
			}
			invalidateSize();
			invalidateDisplayList();
		}
	}
}